#!/user/bin/env python
# -*- coding:utf-8 -*-
# author : 洛月
# email  : 2870093961@qq.com
# created: 2023-09-11 22:31
# updated: 2023-09-11 22:31
from machine import Pin
import lvgl as lv
import time
import config
import fonts


class IPAddressMsgbox:
    mbox = None
    throttle_ms = 400
    press_tick = 0

    def __init__(self, btn):
        btn.irq(self.handle_mbox, trigger=Pin.IRQ_FALLING)

    def handle_mbox(self, _):
        if time.ticks_diff(time.ticks_ms(), self.press_tick) > self.throttle_ms:
            self.press_tick = time.ticks_ms()
            if self.mbox is None:
                text = "--"
                if len(config.IP) > 0:
                    text = config.IP[0]
                print(text)
                self.mbox = lv.msgbox(lv.scr_act(), "IP地址:", text, [], False)
                self.mbox.set_style_text_font(fonts.font_cn_20, 0)
                self.mbox.center()
            else:
                lv.msgbox.close(self.mbox)
                self.mbox = None
